/**    
 * @package com.liusany.myshop.bean.propertyeditor 
 * @description : TODO(用一句话描述该包做什么) 
 * @Copyright: Bmcok Corporation 2012    
*/
package com.liusany.myshop.bean.propertyeditor;

import java.beans.PropertyEditorSupport;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.liusany.modules.utils.Collections3;
import com.liusany.myshop.entity.Role;
import com.liusany.myshop.service.AccountService;

/**   
 * @filename ：RoleListEditor.java   
 * @description : 用于转换用户表单中复杂对象Group的checkbox的关联。
 * @version ：   V 1.0
 * @author : liusany
 * @create : 2012-5-18 上午11:10:44  
 * @Copyright: Bmcok Corporation 2012    
 * 
 * Modification History:
 * 	Date			Author			Version			Description
 *--------------------------------------------------------------
 *2012-5-18 上午11:10:44
 */
@Component
public class RoleListEditor extends PropertyEditorSupport {
	
	@Resource
	private AccountService accountService;
	
	public void setAsText(String text) throws IllegalArgumentException {
		String[] ids = StringUtils.split(text, ",");
		List<Role> roles = new ArrayList<Role>();
		for (String id : ids) {
			Role role = accountService.getRole(Long.valueOf(id));
			roles.add(role);
		}
		setValue(roles);
	}

	@Override
	public String getAsText() {
		return Collections3.extractToString((List) getValue(), "id", ",");
	}

}
